package com.cool.cloud.component;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description 登录拦截器:用于登录检查,权限控制
 * @Author laijunji
 * @Date 15:09 2020/2/26
 * @Param * @param null
 * @return
 **/
public class LoginHandlerInterceptor implements HandlerInterceptor {

    /**
     * 在目标方式执行之前执行
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute("loginUser");
        HttpSession session = request.getSession();
        if (loginUser == null) {
            //未登录,返回登录页面(访问分享链接跟邀请注册不拦截)
            if (request.getRequestURI().equals("/coolcloud/share") || request.getRequestURI().equals("/coolcloud/toregister")) {
                return true;
            }
            response.sendRedirect("/coolcloud/");
            return false;
        } else {

            //页面访问历史记录记入session,便于前端做出显示
            if (request.getRequestURI().contains("index")) {
                session.setAttribute("page", "index");
            } else if (request.getRequestURI().contains("file-all")) {
                session.setAttribute("page", "file-all");
            } else if (request.getRequestURI().contains("file-video")) {
                session.setAttribute("page", "file-video");
            } else if (request.getRequestURI().contains("file-image")) {
                session.setAttribute("page", "file-image");
            } else if (request.getRequestURI().contains("file-doc")) {
                session.setAttribute("page", "file-doc");
            } else if (request.getRequestURI().contains("file-music")) {
                session.setAttribute("page", "file-music");
            } else if (request.getRequestURI().contains("file-other")) {
                session.setAttribute("page", "file-other");
            } else if (request.getRequestURI().contains("myshare")) {
                session.setAttribute("page", "myshare");
            } else if (request.getRequestURI().contains("upload")) {
                session.setAttribute("page", "upload");
            } else if (request.getRequestURI().contains("backgroundwork")) {
                session.setAttribute("page", "backgroundwork");
            } else if (request.getRequestURI().contains("wallet")) {
                session.setAttribute("page", "wallet");
            } else if (request.getRequestURI().contains("manage-user")) {
                session.setAttribute("page", "manage-user");
            } else if (request.getRequestURI().contains("manage-storage")) {
                session.setAttribute("page", "manage-storage");
            } else if (request.getRequestURI().contains("slider-picture")) {
                session.setAttribute("page", "slider-picture");
            }

            //已登录,放行
            return true;
        }
    }
}
